<?php
/**
 * Abstract model class.
 *
 * @category Zend
 * @package Disputeo
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 */

abstract class Disputeo_Model_Table_Row extends Zend_Db_Table_Row_Abstract
{
    /**
     * @var string
     */
    protected static $_dbTable;
    
    /**
     * Allow constructor to accept data as single array
     * 
     * @param array $config
     */
    public function __construct(array $config = array())
    {
        if (! isset($config['table'])) {
            $config = array('data' => $config);
        }
        
        parent::__construct($config);
    }
    
    /**
     * Magic method for setting properties
     * 
     * @param string $columnName
     * @param mixed $value
     */
    public function __set($columnName, $value)
    {
        if (array_key_exists($columnName, $this->_data)) {
            parent::__set($columnName, $value);
        } elseif (property_exists($this, $columnName)) {
            $this->$columnName = $value;
        } else {
            throw new Exception("Undefined property $columnName");
        }
    }
    
    /**
     * Magic method for getting properties
     * 
     * @param string $columnName
     * @return mixed
     */
    public function __get($columnName)
    {
        if (array_key_exists($columnName, $this->_data)) {
            return parent::__get($columnName);
        } else {
            return isset($this->$columnName) ? $this->$columnName : null;
        }
    }
    
    /**
     * Get a select object for the table
     * 
     * @return Zend_Db_Table_Select
     */
    public function select()
    {
        return $this->_table->select()->setIntegrityCheck(false);
    }
    
    /**
     * Create a new record
     * 
     * @param array $data
     * @return \self
     */
    static public function create($data = array())
    {
        $model = static::_model();
        if (! empty($data)) {
            $model->setFromArray($data);
        }
       
        return $model;
    }
    
    /**
     * Get a row object by primary key
     * 
     * @param int $id
     * @return \self
     */
    static public function get($id)
    {
        $model = static::_model();
        $key = current($model->_table->info(Zend_Db_Table::PRIMARY));
        $row = $model->_table->fetchRow(
            $model->select()->where("$key = ?", $id)
        );
        
        return $row;
    }
    
    /**
     * Get model row object for table
     * 
     * @return \self
     * @throws Exception
     */
    static protected function _model()
    {
        if (static::$_dbTable) {
            $tableName = 'Application_Model_DbTable_' . ucfirst(static::$_dbTable);
            $table = new $tableName();
            $modelName = $table->getRowClass();
            $model = new $modelName(array('table' => $table));
            return $model;
        }
        
        throw new Exception('No table name set for row');
    }
}